#include <bits/stdc++.h>
#define int long long
using namespace std;

signed main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    int t;
    cin>>t;
    while(t--){
        int n,a[200005],b[200005],ans=-1;
        cin>>n;
        for(int i=1;i<=n;i++)
            cin>>a[i];
        if(n<=100){
            for(int i=1;i<=(1<<n);i++){
                memset(b,0,sizeof(b));
                int x=i,y=1,sum=0;
                for(int j=1;j<=n;j++){
                    b[y]=x%2;
                    x/=2,y++;
                }
                for(int j=1;j<=n;j++){
                    for(int k=j-1;k>=1;k--){
                        if(b[j]==b[k]){
                            if(a[j]==a[k]) sum+=a[j];
                            goto end;
                        }
                    }
                    end:;
                }
                ans=max(ans,sum);
            }
            cout<<ans<<endl;
        }
        else{
            int dp[200005],cnt=0;
            memset(dp,0,sizeof(dp));
            for(int i=1;i<=n;i++){
                dp[i]=dp[i-1];
                for(int j=i-1;j>=1;j--){
                    if(a[i]==a[j]){
                        dp[i]=max(dp[i],dp[j]+a[i]);
                        goto end1;
                    }
                    end1:;
                }
            }
            cout<<dp[n]<<endl;
        }
    }
    return 0;
}
